<?php
class BrandController extends AdminAppController{
    
    function index(){
        $this->loadModel('Admin.Brand');
        $brand = $this->Brand->get_brand();
        $list_brand = array();
        foreach ($brand as $bra) {
            $temp = array();
            $temp['brand_id'] = $bra['Brand']['brand_id'];
            $temp['title'] = $bra['Brand']['title'];
            $temp['sort_order'] = $bra['Brand']['sort_order'];
            $temp['status'] = $bra['Brand']['status'];
            $temp['group'] = $bra['Brand']['group'];
            $list_brand[] = $temp;
        }
        $this->set('list_brand', $list_brand);
    }
    
    function add(){
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Brand');
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        if ($this->request->is('post')) {
            $brand_id = "";
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => $this->request->data['Brand']['title_'.$lang_code],
                        'description' => $this->request->data['Brand']['description_'.$lang_code],
                        'status' => $_POST["status"],
                        'lang_code' => $lang_code,
                        'group' => 'product',
                        );
                if($brand_id != "")
                    $data['brand_id'] = $brand_id;
                $brand_id = $this->Brand->add($data);
            }
            $this->Session->setFlash(
                                'Thêm mới thương hiệu thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "brand", 
                                  "action" => "index"));
        }
    }
    
    function edit($brand_id = ""){
        if($brand_id == ""){
            $this->redirect(array("controller" => "brand", 
                                  "action" => "index")
                            );
            exit;
        }
        
        $this->set('brand_id', $brand_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Brand');


        $brand_info = $this->Brand->get_brand_by_id($brand_id, 'all');
        $this->set('brand_info', $brand_info);
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        
        if ($this->request->is('post')) {
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => "'".$this->request->data['Brand']['title_'.$lang_code]."'",
                        'description' => "'".$this->request->data['Brand']['description_'.$lang_code]."'",
                        );
                
                $this->Brand->update($brand_id, $lang_code, $data);
            }
            $this->Session->setFlash(
                                'Sửa thương hiệu thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array("controller" => "brand", 
                                  "action" => "index"));
        }
    }
    
}